﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Common.Enums;

namespace ZhimeiSoft.CharmManager.Client.Forms.Control.ServiceItem
{
    public partial class CustomerServiceItemControl : UserControl
    {
        private int iCustomerID = -1;
        private DataSet dsCustomerServiceItem;
        private DataSet dsCustomerServiceItemDetail;
        private string PLEASE_SELECT_SERVICEITEM = "请先选会员的服务项目。"; 
        public CustomerServiceItemControl()
        {
            InitializeComponent();

            ButtonItemIconHelper.InitRibbonBar(this.rbServiceItem);
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar1);

            customSearch1.onSerchCustom = this.SerchCustomInfo;
        }

        /// <summary>
        /// 搜索会员控件返回的会员信息
        /// </summary>
        /// <param name="drCustomInfo"></param>
        private void SerchCustomInfo(DataRow drCustomInfo)
        {
            if (drCustomInfo == null)
            {
                return;
            }

            iCustomerID = Convert.ToInt32(drCustomInfo[DBEntityCustomInfo.FieldName.CUSTOMERID]);

            this.lblName.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CNAME].ToString();
            this.lblNickName.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CNICKNAME].ToString();
            this.lblPhone.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CMOBILEPHONE].ToString();
            this.lblLastAccessDate.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE].ToString();

            InitCustomerServiceItem(iCustomerID);
        }

        private void InitCustomerServiceItem(int customerID)
        {
            DBEntityCustomerServiceItem item = new DBEntityCustomerServiceItem();
            string error = string.Empty;
            string sFilter = string.Format("{0}.{1}={2}", item.GetTableName(), DBEntityCustomerServiceItem.FieldName.CUSTOMERID, customerID.ToString());
            dsCustomerServiceItem = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItem(sFilter, out dsCustomerServiceItem, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo(error);
                return;
            }

            DataColumn dc = new DataColumn("STATENAME");
            dsCustomerServiceItem.Tables[0].Columns.Add(dc);
            foreach (DataRow dr in dsCustomerServiceItem.Tables[0].Rows)
            {
                string state = dr[DBEntityCustomerServiceItem.FieldName.CSTATUS].ToString();
                if (state == DBEntityCustomerServiceItem.ENUM_CSTATUS.C0A)
                    dr["STATENAME"] = DBEntityCustomerServiceItem.ENUM_CSTATUS.C0A_CHINESE;
                else if (state == DBEntityCustomerServiceItem.ENUM_CSTATUS.C0E)
                    dr["STATENAME"] = DBEntityCustomerServiceItem.ENUM_CSTATUS.C0E_CHINESE;
            }
            dsCustomerServiceItem.AcceptChanges();

            gdServiceItem.DataSource = dsCustomerServiceItem.Tables[0].DefaultView;
        }

        private void InitCustomerServiceItemDetail(int iCustomerServiceItemID)
        {
            string error = string.Empty;
            string sFilter = string.Empty;
            bool bFlag = false;
            if (dsCustomerServiceItemDetail == null)
            {
                dsCustomerServiceItemDetail = new DataSet();
                bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItemReservationTime(string.Empty, out dsCustomerServiceItemDetail, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("获取会员服务周期失败\r\n" + error);
                    return;
                }
                gdPlan.DataSource = dsCustomerServiceItemDetail.Tables[0].DefaultView;
            }
            
            if (iCustomerServiceItemID > 0 || iCustomerServiceItemID == -1)
            {
                sFilter = string.Format("{0}={1}", DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMID, iCustomerServiceItemID.ToString());
                dsCustomerServiceItemDetail.Tables[0].DefaultView.RowFilter = sFilter;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (iCustomerID == -1)
            {
                MsgBox.MsgInfo("请先选择会员。");
                return;
            }
            frmCustomerServiceItemAdd form = new frmCustomerServiceItemAdd();
            form.Mode = ControlClassModeEnum.emInsert;
            form.CustomerID = iCustomerID;
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCustomerServiceItem(iCustomerID);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要删除的服务项目。");
                return;
            }

            bool bFlag = MsgBox.MsgQue("确定要删除选中的服务项目吗？");
            if (bFlag == false)
                return;

            DataRow dr = gvServiceItem.GetDataRow(iRowHandle);
            int iCustomerServceItemID = Convert.ToInt32(dr[DBEntityCustomerServiceItem.FieldName.CUSTOMERSERVICEITEMID]);

            string error = string.Empty;
            bFlag = BussinessProxyManager.Instance.DeleteCustomerServiceItem(iCustomerServceItemID, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("删除会员服务项目失败，" + error);
                return;
            }
            else
            {
                InitCustomerServiceItem(iCustomerID);
            }
        }

        private void btnSetPlan_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请先选择服务项目。");
                return;
            }

            DataRow dr = gvServiceItem.GetDataRow(iRowHandle);
            DBEntityCustomerServiceItem item = new DBEntityCustomerServiceItem(dr);

            frmCustomerServicePlan form = new frmCustomerServicePlan(item);
            if (form.ShowDialog() == DialogResult.OK)
            {

            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要修改的服务项目。");
                return;
            }

            DataRow dr = gvServiceItem.GetDataRow(iRowHandle);
            DBEntityCustomerServiceItem item = new DBEntityCustomerServiceItem(dr);
            frmCustomerServiceItemAdd form = new frmCustomerServiceItemAdd(item);
            form.Mode = ControlClassModeEnum.emEdit;
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCustomerServiceItem(iCustomerID);
            }
        }

        private int CheckServiceItemSelected()
        {
            int iResult = -1;
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo(this.PLEASE_SELECT_SERVICEITEM);
                return iResult;
            }
            DataRow drCustomerServiceItem = gvServiceItem.GetDataRow(iRowHandle);
            iResult = Convert.ToInt32(drCustomerServiceItem[DBEntityCustomerServiceItem.FieldName.CUSTOMERSERVICEITEMID]);
            return iResult;
        }

        private void btnReference_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo(this.PLEASE_SELECT_SERVICEITEM);
                return;
            }
            DataRow drCustomerServiceItem = gvServiceItem.GetDataRow(iRowHandle);
            int iCustomerServiceItemID = Convert.ToInt32(drCustomerServiceItem[DBEntityCustomerServiceItem.FieldName.CUSTOMERSERVICEITEMID]);
            int iServiceItemID = Convert.ToInt32(drCustomerServiceItem[DBEntityCustomerServiceItem.FieldName.SERVICEITEMID]);

            bool bFlag = MsgBox.MsgQue("确定要将服务项目标准周期引入到当前会员的服务周期吗？");
            if (bFlag == false)
                return;

            string error = string.Empty;
            DataSet dsStandard = new DataSet();
            bFlag = BussinessProxyManager.Instance.QueryServiceItemDetail(iServiceItemID, out dsStandard, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取最佳服务计划失败\r\n" + error);
                return;
            }

            if (dsStandard.Tables[0].Rows.Count <= 0)
                return;

            foreach (DataRow dr in dsStandard.Tables[0].Rows)
            {
                DataRow drNew = dsCustomerServiceItemDetail.Tables[0].NewRow();
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CSEQNUM] = dr[DBEntityServiceItemDetail.FieldName.SEQNUM];
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CDAYS] = dr[DBEntityServiceItemDetail.FieldName.SDAYS];
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CMEMO] = dr[DBEntityServiceItemDetail.FieldName.SMEMO];
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMID] = iCustomerServiceItemID;
                
                dsCustomerServiceItemDetail.Tables[0].Rows.Add(drNew);
            }

            bFlag = BussinessProxyManager.Instance.SubmitCustomerServiceItmeReservationTime(dsCustomerServiceItemDetail, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("保存会员服务周期失败\r\n" + error);
                return;
            }
            else
            {
                dsCustomerServiceItemDetail = null;
                InitCustomerServiceItemDetail(iCustomerServiceItemID);
            }
        }

        private void btnAddPlan_Click(object sender, EventArgs e)
        {
            int iCustomerServiceItemID = CheckServiceItemSelected();
            if (iCustomerServiceItemID == -1)
                return;

            frmServiceItemPlanAdd form = new frmServiceItemPlanAdd();
            form.Mode = ControlClassModeEnum.emInsert;
            form.Type = 2;
            form.CustomerServiceItemID = iCustomerServiceItemID;
            if (form.ShowDialog() == DialogResult.OK)
            {
                dsCustomerServiceItemDetail = null;
                InitCustomerServiceItemDetail(iCustomerServiceItemID);
            }
        }

        private void btnModifyPlan_Click(object sender, EventArgs e)
        {
            int iCustomerServiceItemID = CheckServiceItemSelected();
            if (iCustomerServiceItemID == -1)
                return;

            int iRowHandle = gvPlan.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要修改的服务周期记录。");
                return;
            }
            DataRow dr = gvPlan.GetDataRow(iRowHandle);
            frmServiceItemPlanAdd form = new frmServiceItemPlanAdd();
            form.Mode = ControlClassModeEnum.emEdit;
            form.Type = 2;
            form.CustomerServiceItemReservationTime = new DBEntityCustomerServiceItemReservationTime(dr);
            if (form.ShowDialog() == DialogResult.OK)
            {
                dsCustomerServiceItemDetail = null;
                InitCustomerServiceItemDetail(iCustomerServiceItemID);
            }
        }

        private void btnDeletePlan_Click(object sender, EventArgs e)
        {
            int iCustomerServiceItemID = CheckServiceItemSelected();
            if (iCustomerServiceItemID == -1)
                return;

            int iRowHandle = gvPlan.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要删除的服务周期记录。");
                return;
            }
            DataRow dr = gvPlan.GetDataRow(iRowHandle);
            int iDetailID = Convert.ToInt32(dr[DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMDETAILID]);
            bool bFlag = MsgBox.MsgQue("确定要删除此服务周期记录吗？");
            if (bFlag == true)
            {
                string error = string.Empty;
                string sFilter = string.Format("{0}={1}", DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMDETAILID, iDetailID.ToString());
                bFlag = BussinessProxyManager.Instance.DeleteCustomerServiceItemReservationTime(sFilter, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("删除服务周期记录失败\r\n" + error);
                    return;
                }
                else
                {
                    dsCustomerServiceItemDetail = null;
                    InitCustomerServiceItemDetail(iCustomerServiceItemID);
                }
            }
        }

        private void gvServiceItem_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                //如果找不到会员服务项目信息，应该刷新最佳服务周期Grid为空
                InitCustomerServiceItemDetail(-1);
                return;
            }

            DataRow drCustomerServiceItem = gvServiceItem.GetDataRow(iRowHandle);
            int iCustomerServiceItemID = Convert.ToInt32(drCustomerServiceItem[DBEntityCustomerServiceItem.FieldName.CUSTOMERSERVICEITEMID]);
            InitCustomerServiceItemDetail(iCustomerServiceItemID);
        }

        private void gvServiceItem_DoubleClick(object sender, EventArgs e)
        {
            btnModify_Click(null, null);
        }

        private void gvPlan_DoubleClick(object sender, EventArgs e)
        {
            btnModifyPlan_Click(null, null);
        }
    }
}
